package com.whpu.mall.product.controller;

import com.whpu.common.utils.PageUtils;
import com.whpu.common.utils.R;
import com.whpu.common.valid.AddGroup;
import com.whpu.common.valid.UpdateGroup;
import com.whpu.common.valid.UpdateStatusGroup;
import com.whpu.mall.product.entity.BrandEntity;
import com.whpu.mall.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 品牌
 *
 * @author FanboKai
 * @email fanbokai163@163.com
 * @date 2020-11-20 22:04:29
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:brand:save")
    public R save(@Validated({AddGroup.class}) @RequestBody BrandEntity brand/*, BindingResult result*/){
        /*      HashMap<String, String> errorMap = new HashMap<>();
        //判断是否有错误信息
        if (result.hasErrors()){
            result.getFieldErrors().forEach((error) -> {
                String field = error.getField();  //错误字段
                String message = error.getDefaultMessage();  //错误信息
                errorMap.put(field,message);
            });
            //返回错误信息
            return R.error(444,"参数不合法").put("data",errorMap);
        }else {
            brandService.save(brand);
        }*/
        brandService.save(brand);

        return R.ok();
    }

    /**
     * 级联修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:brand:update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand){
        //级联更新
		brandService.updateCasc(brand);

        return R.ok();
    }

    /**
     * 修改状态
     */
    @RequestMapping("/update/status")
    //@RequiresPermissions("product:brand:update")
    public R updateStatus(@Validated({UpdateStatusGroup.class}) @RequestBody BrandEntity brand){
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
